package com.itheima.dianming.util;

import cn.hutool.core.io.FileUtil;
import com.itheima.dianming.exception.BusinessException;
import org.eclipse.jgit.api.CloneCommand;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;

import java.io.File;


public class GitUtils {

    /**
     * 克隆远程库
     * @param remotePath 远程仓库地址
     * @param branchName 远程仓库分支
     * @param localPath 克隆到本地路径
     * @throws GitAPIException git异常
     */
    public static void gitClone(String remotePath,String branchName,String localPath) throws Exception {
        boolean dirEmpty = FileUtil.isDirEmpty(new File(localPath));
        //设置远程服务器上的用户名和密码
        UsernamePasswordCredentialsProvider usernamePasswordCredentialsProvider =new
                UsernamePasswordCredentialsProvider("antman123","1qaz@WSX");
        if(dirEmpty){//拉取代码
            //克隆代码库命令
            Git.cloneRepository()
                    .setURI(remotePath) //设置远程URI
                    .setBranch(branchName) //设置clone下来的分支
                    .setDirectory(new File(localPath)) //设置下载存放路径
                    .setCredentialsProvider(usernamePasswordCredentialsProvider) //设置权限验证
                    .call();
        }else {
            Git.open(new File(localPath+"\\.git")).pull();
        }
    }

    public static void main(String[] args) {
        try {
            Git.open(new File("C:\\Temp\\9\\.git")).pull();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

   /* *//**
    * 本地新建仓库
    *//*
    public void testCreate() throws IOException {
        //本地新建仓库地址
        Repository newRepo = FileRepositoryBuilder.create(new File(initPath + "/.git"));
        newRepo.create();
    }

    *//**
    * 本地仓库新增文件
    *//*
    public void testAdd() throws IOException, GitAPIException {
        File myfile = new File(localPath + "/myfile.txt");
        myfile.createNewFile();
        //git仓库地址
        Git git = new Git(new FileRepository(localPath+"/.git"));

        //添加文件
        git.add().addFilepattern("myfile").call();
    }

    *//**
    * 本地提交代码
    *//*
    public void testCommit() throws IOException, GitAPIException,
            JGitInternalException {
        //git仓库地址
        Git git = new Git(new FileRepository(localPath+"/.git"));
        //提交代码
        git.commit().setMessage("test jGit").call();
    }


    *//**
    * 拉取远程仓库内容到本地
    *//*
    public void testPull() throws IOException, GitAPIException {

        UsernamePasswordCredentialsProvider usernamePasswordCredentialsProvider =new
                UsernamePasswordCredentialsProvider("username","password");
        //git仓库地址
        Git git = new Git(new FileRepository(localPath+"/.git"));
        git.pull().setRemoteBranchName("master").
                setCredentialsProvider(usernamePasswordCredentialsProvider).call();
    }

    *//**
    * push本地代码到远程仓库地址
    *//*
    public void testPush() throws IOException, JGitInternalException,
            GitAPIException {

        UsernamePasswordCredentialsProvider usernamePasswordCredentialsProvider =new
                UsernamePasswordCredentialsProvider("username","password");
        //git仓库地址
        Git git = new Git(new FileRepository(localPath+"/.git"));
        git.push().setRemote("origin").     setCredentialsProvider(usernamePasswordCredentialsProvider).call();
    }*/
}
